﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using SysCommon;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;

namespace TMSIM.Client.BizGIS.Business
{
    public class MapSearchTools
    {
        public static void SelectFeaturesScreenPoint(IMap pMap, int x, int y, int pixelTol)
        {
            try
            {
                IEnvelope pEnvelope = CreateSearchEnvelope(pMap, x, y, pixelTol);

                ISelectionEnvironment pSelectionEnvironment = new SelectionEnvironmentClass();

                pSelectionEnvironment.CombinationMethod =
                  esriSelectionResultEnum.esriSelectionResultNew;

                pMap.SelectByShape(pEnvelope, null, false);
            }
            catch (Exception ex)
            {
                Global.mainLog.Error("调用SelectFeaturesScreenPoint出错", ex);
            }
        }


        private static IEnvelope CreateSearchEnvelope(IMap pMap, int x, int y, int pixelTol)
        {
            tagRECT r;
            //Construct a small rectangle out of the x,y coordinates' pixel tolerance.
            r.left = x - pixelTol; //Upper left x, top left is 0,0.  
            r.top = y - pixelTol; //Upper left y, top left is 0,0.
            r.right = x + pixelTol; //Lower right x, top left is 0,0. 
            r.bottom = y + pixelTol; //Lower right y, top left is 0,0.

            //Transform the device rectangle into a geographic rectangle via the display transformation.  
            IEnvelope pEnvelope = new EnvelopeClass();
            IActiveView pActiveView = pMap as IActiveView;
            IDisplayTransformation pDisplayTrans =
              pActiveView.ScreenDisplay.DisplayTransformation;
            pDisplayTrans.TransformRect(pEnvelope, ref r, 5);
            //5 = esriTransformPosition + esriTransformToMap.

            pEnvelope.SpatialReference = pMap.SpatialReference;
            return pEnvelope;
        }
        /// <summary>
        /// 在指定的图层中查找指定点的图元
        /// </summary>
        /// <param name="map">图层所在地图</param>
        /// <param name="flyr">图层</param>
        /// <param name="x">点坐标</param>
        /// <param name="y">点坐标</param>
        /// <param name="pixelTol">搜索范围</param>
        /// <returns>图元集</returns>
        public static int[] SearchFeaturesByPoint(IMap map, IFeatureLayer flyr, int x, int y, int pixelTol)
        {
            if (map == null || flyr == null)
            {
                return null;
            }

            string oldField = string.Empty;

            try
            {
                if (flyr != null)
                {
                    oldField = flyr.DisplayField;
                    IFeatureClass featCls = flyr.FeatureClass;
                    if (featCls == null)
                    {
                        return null;
                    }
                    flyr.DisplayField = "OBJECTID";
                    IIdentify identify = flyr as IIdentify;
                    if (identify != null)
                    {
                        //转换点
                        IEnvelope env = CreateSearchEnvelope(map, x, y, pixelTol);
                        IArray array = identify.Identify((IGeometry)env);

                        if (array != null)
                        {
                            List<int> lst = new List<int>(array.Count);
                            for (int i = 0; i < array.Count; i++)
                            {
                                IFeatureIdentifyObj featObj = array.get_Element(i) as IFeatureIdentifyObj;
                                IIdentifyObj obj = featObj as IIdentifyObj;
                                if (obj != null)
                                {
                                    int index = int.Parse(obj.Name);
                                    lst.Add(index);
                                }
                            }
                            array.RemoveAll();
                            array = null;
                            return lst.ToArray();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error(
                    "调用SearchFeatureOIDByPoint(IFeatureLayer flyr, int x, int y, int pixelTol)出错",
                    ex);
            }
            finally
            {
                if (flyr != null && oldField != string.Empty)
                {
                    flyr.DisplayField = oldField;
                }
            }
            return null;
        }
        public static int[] SearchFeaturesByPoint(IMap map, IFeatureLayer flyr, IEnvelope env)
        {
            if (map == null || flyr == null || env==null)
            {
                return null;
            }

            string oldField = string.Empty;

            try
            {
                if (flyr != null)
                {
                    oldField = flyr.DisplayField;
                    IFeatureClass featCls = flyr.FeatureClass;
                    if (featCls == null)
                    {
                        return null;
                    }
                    flyr.DisplayField = "OBJECTID";
                    IIdentify identify = flyr as IIdentify;
                    if (identify != null)
                    {
                        //转换点
                        //IEnvelope env = CreateSearchEnvelope(map, x, y, pixelTol);
                        IArray array = identify.Identify((IGeometry)env);

                        if (array != null)
                        {
                            List<int> lst = new List<int>(array.Count);
                            for (int i = 0; i < array.Count; i++)
                            {
                                IFeatureIdentifyObj featObj = array.get_Element(i) as IFeatureIdentifyObj;
                                IIdentifyObj obj = featObj as IIdentifyObj;
                                if (obj != null)
                                {
                                    int index = int.Parse(obj.Name);
                                    lst.Add(index);
                                }
                            }
                            array.RemoveAll();
                            array = null;
                            return lst.ToArray();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error(
                    "调用SearchFeatureOIDByPoint(IFeatureLayer flyr, int x, int y, int pixelTol)出错",
                    ex);
            }
            finally
            {
                if (flyr != null && oldField != string.Empty)
                {
                    flyr.DisplayField = oldField;
                }
            }
            return null;
        }

        static void SelectLayersFeatures(IFeatureLayer pFeatureLayer, string WhereClause)
        {
            IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
            if (pFeatureSelection != null)
            {
                IQueryFilter pQueryFilter = new QueryFilterClass();
                pQueryFilter.WhereClause = WhereClause;
                pFeatureSelection.SelectFeatures(pQueryFilter,
                  esriSelectionResultEnum.esriSelectionResultNew, false);
            }
        }

        static void SelectFeaturesPolygon(IMap pMap, IPolygon pPolygon)
        {
            ISelectionEnvironment pSelectionEnvironment = new SelectionEnvironmentClass();
            pSelectionEnvironment.CombinationMethod =
              esriSelectionResultEnum.esriSelectionResultNew;
            pMap.SelectByShape(pPolygon, pSelectionEnvironment, false);
        }

        public static void ClearMapSelection(IMap pMap)
        {
            try
            {
                pMap.ClearSelection();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用ClearMapSelection(IMap pMap)出错", ex);
            }
        }

        static void ClearLayerSelection(IFeatureLayer pFeatureLayer)
        {
            IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
            if (pFeatureSelection != null)
            {
                pFeatureSelection.Clear();
            }
        }
    }
}
